package com.atguigu;

import com.atguigu.dao.CustomerDao;
import com.atguigu.domain.Customer;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@RunWith(SpringJUnit4ClassRunner.class) //声明spring提供的单元测试环境
@ContextConfiguration(locations = "classpath:applicationContext.xml")//指定spring容器的配置信息
public class CustomerDaoTest {

    @Autowired
    private CustomerDao customerDao;

    /**
     * 根据id查询
     */
    @Test
    public void test01() {
        Customer customer = customerDao.findOne(3l);
        System.out.println(customer);
    }


    /**
     * save : 保存或者更新
     * 根据传递的对象是否存在主键id，
     * 如果没有id主键属性：保存
     * 存在id主键属性，根据id查询数据，更新数据
     */
    @Test
    public void test02() {
        Customer c = new Customer();
        c.setCustName("凯爹");
        c.setCustSource("研发部");
        c.setCustIndustry("IT");
        c.setCustLevel("VIP客户");
        c.setCustPhone("123456");
        c.setCustAddress("美国白宫");
        customerDao.save(c);


    }

    //修改
    @Test
    public void test03() {
        Customer c = new Customer();
        c.setCustId(10L);
        c.setCustName("凯神");
        c.setCustSource("研发部");
        c.setCustIndustry("IT");
        c.setCustLevel("VIP客户");
        c.setCustPhone("123456");
        c.setCustAddress("美国白宫");
        customerDao.save(c);
    }

    //删除
    @Test
    public void test04() {
        customerDao.delete(10l);
    }

    /**
     * 查询所有
     */
    @Test
    public void test05() {
        List<Customer> list = customerDao.findAll();
        for (Customer customer : list) {
            System.out.println(customer);
        }
    }


    /**
     * 查询所有（排序）
     * Sort.Direction.AES：升序
     * Sort.Direction.DESC：降序
     */
    @Test
    public void test06() {
        Sort sort = new Sort(Sort.Direction.ASC, "custId");
        List<Customer> list = customerDao.findAll(sort);
        for (Customer customer : list) {
            System.out.println(customer);
        }
    }


    /**
     * 查询所有（分页+排序）
     */
    @Test
    public void test07() {
//        Sort sort = new Sort(Sort.Direction.DESC,"custId");
        /**
         * PageRequest(int page, int size)
         * page：表示当前页，0表示基准页，0表示第1页
         * size：表示当前页最多显示的记录数
         */
        Pageable pageable = new PageRequest(0, 2);
        Page<Customer> pageData = customerDao.findAll(pageable);
        System.out.println("总记录数：" + pageData.getTotalElements());
        System.out.println("总页数：" + pageData.getTotalPages());
        for (Customer customer : pageData.getContent()) {
            System.out.println(customer);
        }
    }


    /**
     * 测试统计查询：查询客户的总数量
     * count:统计总条数
     */
    @Test
    public void test08() {
        long count = customerDao.count();//查询全部的客户数量
        System.out.println(count);
    }


    /**
     * 测试：判断id为4的客户是否存在
     * 1. 可以查询以下id为4的客户
     * 如果值为空，代表不存在，如果不为空，代表存在
     * 2. 判断数据库中id为4的客户的数量
     * 如果数量为0，代表不存在，如果大于0，代表存在
     */
    @Test
    public void test09() {
        boolean exists = customerDao.exists(3l);
        System.out.println("id为4的客户 是否存在：" + exists);
    }


    /**
     * 根据id从数据库查询
     *
     * @Transactional : 保证getOne正常运行
     * <p>
     * findOne：
     * em.find()           :立即加载
     * getOne：
     * em.getReference     :延迟加载
     * * 返回的是一个客户的动态代理对象
     * * 什么时候用，什么时候查询
     */
    @Test
    @Transactional
    public void test10() {
        Customer customer = customerDao.getOne(7l);
        System.out.println(customer);
    }


    // 查询所有
    @Test
    public void test11() {
        List<Customer> list = customerDao.findAllCustomer();
        for (Customer customer : list) {
            System.out.println(customer);
        }
    }


    // 按照名称模糊查询
    @Test
    public void test12() {
        String custName = "%凯%";
        List<Customer> list = customerDao.findCustNameLike(custName);
        for (Customer customer : list) {
            System.out.println(customer);
        }
    }

    // 按照客户名称模糊查询，同时按照客户所属行业匹配查询
    @Test
    public void test13() {
        String custName = "%凯%";
        String custIndustry = "IT";
        List<Customer> list = customerDao.findCustNameLikeAndCustIndustry(custName, custIndustry);
        for (Customer customer : list) {
            System.out.println(customer);
        }
    }



    // 查询所有sql
    // 查询所有
    @Test
    public void test14(){
        List<Customer> list = customerDao.findAllCustomerSQL();
        for (Customer customer : list) {
            System.out.println(customer);
        }
    }

    // 按照名称模糊查询
    @Test
    public void test15(){
        String custName = "%凯%";
        List<Customer> list = customerDao.findCustNameLikeSQL(custName);
        for (Customer customer : list) {
            System.out.println(customer);
        }
    }
    // 按照客户名称模糊查询，同时按照客户所属行业匹配查询
    @Test
    public void test16(){
        String custName = "%凯%";
        String custIndustry = "IT";
        List<Customer> list = customerDao.findCustNameLikeAndCustIndustrySQL(custName,custIndustry);
        for (Customer customer : list) {
            System.out.println(customer);
        }
    }


    // 测试，更新custAddress字段
    @Test
    @Transactional // 添加事务处理，增删改操作事务是必须的
    //@Commit// 默认操作完成之后，事务是回滚的，要想提交，需要手动提交
    @Rollback(false) // 事务不回滚
    public void test17(){
        String custAddress = "美国六角大楼";
        Long custId = 3L;
        customerDao.updateCustomerCustAddress(custAddress,custId);
    }

    // 按照名称查询
    @Test
    public void test18(){
        String custName = "凯哥";
        List<Customer> list = customerDao.findByCustName(custName);
        for (Customer customer : list) {
            System.out.println(customer);
        }
    }

    // 按照客户名称模糊查询
    @Test
    public void test19(){
        String custName = "%凯%";
        List<Customer> list = customerDao.findByCustNameLike(custName);
        for (Customer customer : list) {
            System.out.println(customer);
        }
    }

    // 按照客户名称模糊查询+客户的所属行业
    @Test
    public void test20(){
        String custName = "%凯%";
        String custIndustry = "IT";
        List<Customer> list = customerDao.findByCustNameLikeAndCustIndustry(custName,custIndustry);
        for (Customer customer : list) {
            System.out.println(customer);
        }
    }

}